﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace YJ.Data.MySql
{
    public class AppLibraryButtons : YJ.Data.Interface.IAppLibraryButtons
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public AppLibraryButtons()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.AppLibraryButtons实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.AppLibraryButtons model)
        {
            string sql = @"INSERT INTO applibrarybuttons
				(ID,Name,Events,Ico,Sort,Note) 
				VALUES(@ID,@Name,@Events,@Ico,@Sort,@Note)";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID },
				new MySqlParameter("@Name", MySqlDbType.VarChar, 50){ Value = model.Name },
				new MySqlParameter("@Events", MySqlDbType.Text, -1){ Value = model.Events },
				model.Ico == null ? new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = model.Ico },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort },
				model.Note == null ? new MySqlParameter("@Note", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Note", MySqlDbType.Text, -1) { Value = model.Note }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.AppLibraryButtons实体类</param>
        public int Update(YJ.Data.Model.AppLibraryButtons model)
        {
            string sql = @"UPDATE applibrarybuttons SET 
				Name=@Name,Events=@Events,Ico=@Ico,Sort=@Sort,Note=@Note
				WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@Name", MySqlDbType.VarChar, 50){ Value = model.Name },
				new MySqlParameter("@Events", MySqlDbType.Text, -1){ Value = model.Events },
				model.Ico == null ? new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = model.Ico },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort },
				model.Note == null ? new MySqlParameter("@Note", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Note", MySqlDbType.Text, -1) { Value = model.Note },
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM applibrarybuttons WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.AppLibraryButtons> DataReaderToList(MySqlDataReader dataReader)
        {
            List<YJ.Data.Model.AppLibraryButtons> List = new List<YJ.Data.Model.AppLibraryButtons>();
            YJ.Data.Model.AppLibraryButtons model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.AppLibraryButtons();
                model.ID = dataReader.GetString(0).ToGuid();
                model.Name = dataReader.GetString(1);
                model.Events = dataReader.GetString(2);
                if (!dataReader.IsDBNull(3))
                    model.Ico = dataReader.GetString(3);
                model.Sort = dataReader.GetInt32(4);
                if (!dataReader.IsDBNull(5))
                    model.Note = dataReader.GetString(5);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.AppLibraryButtons> GetAll()
        {
            string sql = "SELECT * FROM applibrarybuttons";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.AppLibraryButtons> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM applibrarybuttons";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.AppLibraryButtons Get(Guid id)
        {
            string sql = "SELECT * FROM applibrarybuttons WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.AppLibraryButtons> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }


        /// <summary>
        /// 得到一页数据
        /// </summary>
        /// <param name="pager"></param>
        /// <param name="query"></param>
        /// <param name="order"></param>
        /// <param name="size"></param>
        /// <param name="numbe"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public List<YJ.Data.Model.AppLibraryButtons> GetPagerData(out string pager, string query = "", int size = 15, int number = 1, string title = "")
        {
            StringBuilder WHERE = new StringBuilder();
            List<MySqlParameter> parList = new List<MySqlParameter>();
            if (!title.IsNullOrEmpty())
            {
                WHERE.Append("AND INSTR(Name,@Name)>0 ");
                parList.Add(new MySqlParameter("@Name", MySqlDbType.VarChar) { Value = title });
            }
           
            long count;
            string sql = dbHelper.GetPaerSql("AppLibraryButtons", "*", WHERE.ToString(), "Sort DESC", size, number, out count, parList.ToArray());

            pager = YJ.Utility.Tools.GetPagerHtml(count, size, number, query);
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parList.ToArray());
            List<YJ.Data.Model.AppLibraryButtons> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }

        /// <summary>
        /// 得到一页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="query"></param>
        /// <param name="title"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public List<YJ.Data.Model.AppLibraryButtons> GetPagerData(out long count, int size, int number, string title = "", string order = "")
        {
            StringBuilder WHERE = new StringBuilder();
            List<MySqlParameter> parList = new List<MySqlParameter>();
            if (!title.IsNullOrEmpty())
            {
                WHERE.Append("AND INSTR(Name,@Name)>0 ");
                parList.Add(new MySqlParameter("@Name", MySqlDbType.VarChar) { Value = title });
            }
            string sql = dbHelper.GetPaerSql("AppLibraryButtons", "*", WHERE.ToString(), order.IsNullOrEmpty() ? "Sort DESC" : order, size, number, out count, parList.ToArray());

            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parList.ToArray());
            List<YJ.Data.Model.AppLibraryButtons> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }

        /// <summary>
        /// 得到最大编号
        /// </summary>
        /// <returns></returns>
        public int GetMaxSort()
        {
            string sql = "SELECT IFNULL(MAX(Sort),0)+5 FROM AppLibraryButtons";
            return dbHelper.GetFieldValue(sql).ToInt();
        }
    }
}